package com.java.controller;

import com.java.bean.*;
import com.java.mvc.ResponseBody;
import com.java.service.CourierService;
import com.java.service.ExpressService;
import com.java.util.DateFormatUtil;
import com.java.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        //1、获取查询数据的起始值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2、获取当前要查询的页数
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3、进行查询
        List<Courier> list = CourierService.findAll(true,offset,pageNumber);
        List<BootStrapTableCourier> list1 = new ArrayList<>();
        for (Courier c:list) {
            String regTime = DateFormatUtil.format(c.getRegTime());
            String preTime = c.getPreTime()==null?"--": DateFormatUtil.format(c.getPreTime());
            String sendNumber = null;
            int courierSendNumber = c.getCourierSendNumber();
            if(courierSendNumber <= 100000){
                sendNumber = courierSendNumber + "";
            }else {
                sendNumber = courierSendNumber/100000 +"w+";
            }
            BootStrapTableCourier c2 = new BootStrapTableCourier(c.getId(),c.getCourierName(),c.getCourierPhone(),c.getCourierIdNumber(),c.getCourierPassword(),sendNumber,regTime,preTime);
            list1.add(c2);
        }
        List<Map<String, Integer>> console = CourierService.console();
        Integer total = console.get(0).get("courier_size");
        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String courierName = request.getParameter("courierName");
        String courierPhone = request.getParameter("courierPhone");
        String courierIdNumber = request.getParameter("courierIdNumber");
        String courierPassword = request.getParameter("courierPassword");
        Courier c = new Courier();
        c.setCourierName(courierName);
        c.setCourierPhone(courierPhone);
        c.setCourierIdNumber(courierIdNumber);
        c.setCourierPassword(courierPassword);
        boolean flag = CourierService.insert(c);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("快递员录入成功！");
        }else {
           msg.setStatus(-1);
           msg.setResult("快递员录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String courierPhone = request.getParameter("courierPhone");
        Courier courier = CourierService.findByPhone(courierPhone);
        Message msg = new Message();
        if(courier == null){
            msg.setStatus(-1);
            msg.setResult("未查找到该快递员信息！");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功！");
            msg.setData(courier);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String courierName = request.getParameter("courierName");
        String courierPhone = request.getParameter("courierPhone");
        String courierIdNumber = request.getParameter("courierIdNumber");
        String courierPassword = request.getParameter("courierPassword");
        Courier newCourier = new Courier();
        newCourier.setCourierName(courierName);
        newCourier.setCourierPhone(courierPhone);
        newCourier.setCourierIdNumber(courierIdNumber);
        newCourier.setCourierPassword(courierPassword);
        boolean flag = CourierService.update(id, newCourier);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = CourierService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
